import request, { all } from '@/utils/request'

/**
 * 并发请求
 * @param paths
 * @param callback
 * @param error
 */
export function requestAll(paths, callback, error = () => {}) {
  const urls = []
  for (let i = 0; i < paths.length; i++) {
    const item = paths[i]
    if (item.url) {
      const config = {}
      if (item.params) {
        config.params = item.params
      }
      if (item.method === 'post') {
        urls.push(request.post(item.url, config.params))
      } else if (item.method === 'delete') {
        urls.push(request.delete(item.url, config))
      } else {
        urls.push(request.get(item.url, config))
      }
    }
  }
  all(urls, function(res) {
    if (res !== false) {
      callback(res)
    } else {
      error(false)
    }
  })
}

/**
 * 七牛密钥
 * @param params
 * @returns {*}
 */
export function assist(params) {
  return request({
    url: 'assist',
    method: 'post',
    data: params,
    prefix: false
  })
}

/**
 * 七牛密钥
 * @param mode
 * @param phone
 * @returns {*}
 */
export function uploadToken(mode, phone) {
  return request({
    url: 'upload/qiniu/token?refresh_cache=1',
    method: 'get',
    prefix: false
  })
}

/**
 * 上传七牛云
 * @param params
 * @param uploadToken
 * @returns AxiosPromise
 */
export function uploadQiNiu(params, uploadToken = null) {
  return request({
    baseURL: 'https://upload-z2.qiniup.com/putb64/-1',
    method: 'post',
    notAuth: true,
    headers: {
      'Content-Type': 'application/octet-stream',
      'Authorization': 'UpToken ' + uploadToken
    },
    data: params
  })
}

/**
 * 获取短信
 * @returns AxiosPromise
 */
export function captcha(mode, phone) {
  return request({
    url: '/sms/captcha',
    method: 'post',
    data: { mode: mode, phone: phone },
    prefix: false
  })
}

/**
 * 获取短信配置
 * @returns AxiosPromise
 */
export function upload(param) {
  return request({
    url: '/file/upload',
    method: 'post',
    data: param,
    prefix: false
  })
}

/**
 * 交易查询
 * @returns AxiosPromise
 */
export function transactionQuery(param) {
  return request({
    url: '/transaction/query',
    method: 'post',
    data: param,
    prefix: false
  })
}

/**
 * 通知轮询
 * @returns AxiosPromise
 */
export function notice(param) {
  return request({
    url: '/new/notice',
    method: 'post',
    data: param,
    prefix: false
  })
}
